package cn.wh.beans.factory.support;

import cn.wh.beans.factory.BeanFactory;
import cn.wh.beans.factory.config.BeanDefinition;

/**
 * @author: wh
 * @create: 2022-08-19 16:27
 * @description
 **/
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {
    @Override
    public Object getBean(String beanName) {
        return doGetBean(beanName, null);
    }

    @Override
    public Object getBean(String beanName, Object... args) {
        return doGetBean(beanName, args);
    }

    public Object doGetBean(String beanName, Object... args) {
        //首先在单例容器中获取bean
        Object bean = getSingleton(beanName);
        if (bean == null) {
            //在容器中获取bean
            BeanDefinition beanDefintion = getBeanDefintion(beanName);
            bean = creatBean(beanName, beanDefintion, args);
        }

        return bean;
    }

    protected abstract BeanDefinition getBeanDefintion(String beanName);

    protected abstract Object creatBean(String beanName, BeanDefinition beanDefinition, Object... args);
}
